﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections; // for stack
using iITT.iITT_Services; // for widget

namespace iITT
{
    public class TemporaryList
    {
        // Because the page refreshes every time a Widget is added, the list.RemoveAt(index) will not interfere with
        // setting the list items to the end of the list and removing them at a certain index.

        private List<Widget> list_widget = new List<Widget>(); // Temporary list to compare to new list.
        private Stack tempStack = new Stack(); // used as a GUID for the controls in each div.

        public Widget getTempWidget(string controlID)
        {
            // return the information from the old widget.
            // Used to compare to new widget.
            Widget tempWidget = new Widget();
            tempWidget = list_widget[Convert.ToInt32(controlID)];
            return tempWidget;
        }

        public void setTempWidget(Widget widget)
        {
            // Add the widget to the temporary list.
            list_widget.Add(widget);
        }

        public string returnID()
        {
            // return the unique ID from the stack.
            string ID = tempStack.Pop().ToString();
            return ID;
        }

        public string removeTempWidget(int controlID)
        {
            list_widget.RemoveAt(controlID);
            // Push the ID back into the stack for reuse then assign the control a GUID() to be safe.
            tempStack.Push(controlID);
            return new Guid().ToString();
        }

        public void clearTempWidgetList()
        {
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    list_widget.RemoveAt(i); // Remove all widgets from list
                    tempStack.Pop(); // Pop all values from stack.
                }
                catch (IndexOutOfRangeException)
                {
                }
            }
        }

        public void initializeStack()
        {
            for (int i = 1; i < 10; i++)
            {
                tempStack.Push(i);
            }
        }
    }
}